package net.pulseproject.router.debuggerapp.beatsim.prototype;

import javax.swing.JPanel;

// see: http://www.java2s.com/Code/Java/Development-Class/Democodewhichplotsthememoryusagebyallmemorypools.htm

/**
 * Demo code which plots the memory usage by all memory pools. The memory usage
 * is sampled at some time interval using java.lang.management API. This demo
 * code is modified based java2d MemoryMonitor demo.
 */
public class SecondPrototype extends JPanel {

	private static final long serialVersionUID = -6826221758517445735L;
/*
	private static final Random R = new Random();
	
  public Surface surf;

  JPanel controls;

  boolean doControls;

  JTextField tf;

  public static void main(String s[]) {
    final SecondPrototype demo = new SecondPrototype();
    WindowListener l = new WindowAdapter() {
      @Override public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
      @Override public void windowDeiconified(WindowEvent e) {
        demo.surf.start();
      }
      @Override public void windowIconified(WindowEvent e) {
        demo.surf.stop();
      }
    };
    JFrame f = new JFrame("MemoryMonitor");
    f.addWindowListener(l);
    f.getContentPane().add("Center", demo);
    f.pack();
    f.setSize(new Dimension(400, 500));
    f.setVisible(true);
    demo.surf.start();
  }
  
  public SecondPrototype() {
    setLayout(new BorderLayout());
    setBorder(new TitledBorder(new EtchedBorder(), "Memory Monitor"));
    add(surf = new Surface());
    controls = new JPanel();
    controls.setPreferredSize(new Dimension(135, 80));
    Font font = new Font("serif", Font.PLAIN, 10);
    JLabel label = new JLabel("Sample Rate");
    label.setFont(font);
    label.setForeground(Color.red);
    controls.add(label);
    tf = new JTextField("1000");
    tf.setPreferredSize(new Dimension(45, 20));
    controls.add(tf);
    controls.add(label = new JLabel("ms"));
    label.setFont(font);
    label.setForeground(Color.red);
    addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        removeAll();
        if ((doControls = !doControls)) {
          surf.stop();
          add(controls);
        } else {
          try {
            surf.sleepAmount = Long.parseLong(tf.getText().trim());
          } catch (Exception ex) {
          }
          surf.start();
          add(surf);
        }
        validate();
        repaint();
      }
    });
  }

  public class Surface extends JPanel implements Runnable {

    public Thread thread;

    public long sleepAmount = 1000;

    public int usageHistCount = 20000;

    private int w, h;

    private BufferedImage bimg;

    private Graphics2D big;

    private Font font = new Font("Times New Roman", Font.PLAIN, 11);

    private int columnInc;

    private float usedMem[][];

    private int ptNum[];

    private int ascent, descent;

    private Rectangle graphOutlineRect = new Rectangle();

    private Rectangle2D mfRect = new Rectangle2D.Float();

    private Rectangle2D muRect = new Rectangle2D.Float();

    private Line2D graphLine = new Line2D.Float();

    private Color graphColor = new Color(46, 139, 87);

    private Color mfColor = new Color(0, 100, 0);

    private String usedStr;

    public Surface() {
      setBackground(Color.black);
      addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
          if (thread == null)
            start();
          else
            stop();
        }
      });
      int i = 0;
//      usedMem = new float[numPools][];
//      ptNum = new int[numPools];
    }

    public Dimension getMinimumSize() {
      return getPreferredSize();
    }

    public Dimension getMaximumSize() {
      return getPreferredSize();
    }

    public Dimension getPreferredSize() {
      return new Dimension(135, 80);
    }

    public void paint(Graphics g) {
      if (big == null) {
        return;
      }

      big.setBackground(getBackground());
      big.clearRect(0, 0, w, h);

      // .. Draw allocated and used strings ..
      big.setColor(Color.green);

      int x1 = 0;
      int x2 = w;
      int y1 = 0;
      int y2 = h;
      // Print Max memory allocated for this memory pool.
      big.drawString("Router", x1 + 4.0f, (float) y1 + ascent + 0.5f);
      big.setColor(Color.yellow);

      // Print the memory pool name.
      big.drawString("Ausschlagsview", x1 + x2 / 2, (float) y1 + ascent + 0.5f);

      // Print the memory used by this memory pool.
      usedStr = "Simulator";
      big.setColor(Color.green);
      big.drawString(usedStr, x1 + 4, y1 + y2 - descent);

      // Calculate remaining size
      float ssH = ascent + descent;
      float remainingHeight = (float) (y2 - (ssH * 2) - 0.5f);
      float blockHeight = remainingHeight / 10;
      float blockWidth = 20.0f;
      float remainingWidth = (float) (x2 - blockWidth - 10);
      
      
//      h = h / ((numPools + numPools % 2) / 2);
//      w = w / 2;
      
   // .. Draw Border ..
      if (remainingWidth <= 30) remainingWidth = (float) 30;
      if (remainingHeight <= ssH) remainingHeight = (float) ssH;
      big.setColor(graphColor);
      int graphX = x1 + 30;
      int graphY = y1 + (int) ssH;
      int graphW = (int) remainingWidth;
      int graphH = (int) remainingHeight;
      graphOutlineRect.setRect(graphX, graphY, graphW, graphH);
      big.draw(graphOutlineRect);


      // .. Draw animated column movement ..
      int graphColumn = graphW / 15;
      if (columnInc == 0) {
        columnInc = graphColumn;
      }
      for (int j = graphX + columnInc; j < graphW + graphX; j += graphColumn) {
        graphLine.setLine(j, graphY, j, graphY + graphH);
        big.draw(graphLine);
      }
      

      big.setColor(Color.yellow);
      int w1 = graphW; // width of memory usage history.
       
      big.fillRect(0, R.nextInt(100), time, 1);
//      for (int j = graphX + graphW - w1, k = ptNum[npool] - w1; k < ptNum[npool]; k++, j++) {
//            if (usedMem[npool][k] != usedMem[npool][k - 1]) {
//              int h1 = (int) (graphY + graphH * ((totalMemory - usedMem[npool][k - 1]) / totalMemory));
//              int h2 = (int) (graphY + graphH * ((totalMemory - usedMem[npool][k]) / totalMemory));
//              big.drawLine(j - 1, h1, j, h2);
//            } else {
//              int h1 = (int) (graphY + graphH * ((totalMemory - usedMem[npool][k]) / totalMemory));
//              big.fillRect(j, h1, 1, 1);
//            }
//        }
      
      time += 30;
      
      g.drawImage(bimg, 0, 0, this);
    }
    private int time = 0;

//    public void plotMemoryUsage(int x1, int y1, int x2, int y2, int npool) {
//      MemoryPoolMXBean mp = mpools.get(npool);
//      float usedMemory = mp.getUsage().getUsed();
//      float totalMemory = mp.getUsage().getMax();
//
//	[ ... ]
//
//      --columnInc;
//
//      // Plot memory usage by this memory pool.
//      if (usedMem[npool] == null) {
//        usedMem[npool] = new float[usageHistCount];
//        ptNum[npool] = 0;
//      }
//
//      // save memory usage history.
//      usedMem[npool][ptNum[npool]] = usedMemory;
//
//
//      [ ... ]
//      
//      if (ptNum[npool] + 2 == usedMem[npool].length) {
//        // throw out oldest point
//        for (int j = 1; j < ptNum[npool]; j++) {
//          usedMem[npool][j - 1] = usedMem[npool][j];
//        }
//        --ptNum[npool];
//      } else {
//        ptNum[npool]++;
//      }
//    }

    public void start() {
      thread = new Thread(this);
      thread.setPriority(Thread.MIN_PRIORITY);
      thread.setName("MemoryMonitor");
      thread.start();
    }

    public synchronized void stop() {
      thread = null;
      notify();
    }

    public void run() {
    	System.out.println("Surface.run()");
      Thread me = Thread.currentThread();

      while (thread == me && !isShowing() || getSize().width == 0) {
        try {
          thread.sleep(500);
        } catch (InterruptedException e) {
          return;
        }
      }

      while (thread == me && isShowing()) {
        Dimension d = getSize();
//        System.out.println("surfice dimension=" + d);
        if (d.width != w || d.height != h) {
          w = d.width;
          h = d.height;
          bimg = (BufferedImage) createImage(w, h);
          big = bimg.createGraphics();
          big.setFont(font);
          FontMetrics fm = big.getFontMetrics(font);
          ascent = (int) fm.getAscent();
          descent = (int) fm.getDescent();
        }
        repaint();
        try {
          thread.sleep(sleepAmount);
        } catch (InterruptedException e) {
          break;
        }
      }
      thread = null;
    }
  }
*/
}
